<template>
<!-- 实验室列表 -->
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="10vw">
      <el-form-item label="实验室名称" prop="labName">
        <el-input v-model="queryParams.labName" placeholder="请输入实验室名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="实验室类别" prop="labCategory">
        <el-select
          v-model="queryParams.labCategory"
          placeholder="请选择实验室类别"
          clearable
          @change="handleQuery"
        >
          <el-option label="软件实验室" value="1" />
          <el-option label="硬件实验室" value="2" />
          <el-option label="网络实验室" value="3" />
        </el-select>
      </el-form-item>

      
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['lab:laboratory:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['lab:laboratory:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['lab:laboratory:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['lab:laboratory:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="laboratoryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="labId" />
      <el-table-column label="实验室编号" align="center" prop="labNumber" />
      <el-table-column label="实验室名称" align="center" prop="labName" />
      <el-table-column label="实验室类别" align="center" prop="labCategory" >
        <template slot-scope="scope">
          {{ getCategoryName(scope.row.labCategory) }}
        </template>
      </el-table-column>
      <el-table-column label="实验设备数" align="center" prop="equipmentCount" />
      <el-table-column label="实验员" align="center" prop="labAssistantName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['lab:laboratory:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['lab:laboratory:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改实验室存储管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="10vw">
        <el-form-item label="实验室编号" prop="labNumber">
          <el-input v-model="form.labNumber" placeholder="请输入实验室编号" />
        </el-form-item>
        <el-form-item label="实验室名称" prop="labName">
          <el-input v-model="form.labName" placeholder="请输入实验室名称" />
        </el-form-item>
        <el-form-item label="实验室类别" prop="labCategory">
          <!-- <el-input v-model="form.labCategory" placeholder="请输入实验室类别" /> -->
          <el-select v-model="form.labCategory" placeholder="请选择实验室类别">
            <el-option label="软件实验室" value="1" />
            <el-option label="硬件实验室" value="2" />
            <el-option label="网络实验室" value="3" />
            <!-- Add more options as needed -->
          </el-select>
        </el-form-item>
        <el-form-item label="实验设备数" prop="equipmentCount">
          <el-input v-model="form.equipmentCount" placeholder="请输入实验设备数" />
        </el-form-item>
        <el-form-item label="实验员" prop="labAssistantId">
          <!-- <el-input v-model="form.labAssistantId" placeholder="请输入实验员" /> -->
          <el-select v-model="form.labAssistantId" placeholder="请选择实验员">
            <el-option :label="item.userName" :value="item.userId" v-for="(item,index) in userList" :key="index" v-if="item.roleId!=2" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLaboratory, getLaboratory, delLaboratory, addLaboratory, updateLaboratory } from "@/api/lab/laboratory";
import {listUser} from "@/api/system/user";

export default {
  name: "Laboratory",
  data() {
    return {
      selectedValues: {
        0: '未审核',
        1: '通过',
        2: '驳回',
        3: '使用完毕'
      },
      userList:[],
      labOption:[
      { value: 1, label: '软件实验室' },
      { value: 2, label: '硬件实验室' },
      { value: 3, label: '网络实验室' },
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 实验室存储管理表格数据
      laboratoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        labName: null,
        labCategory: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        labNumber: [
          { required: true, message: "实验室编号不能为空", trigger: "blur" }
        ],
        labName: [
          { required: true, message: "实验室名称不能为空", trigger: "blur" }
        ],
        labCategory: [
          { required: true, message: "实验室类别不能为空", trigger: "blur" }
        ],
        equipmentCount: [
          { required: true, message: "实验设备数不能为空", trigger: "blur" }
        ],
        labAssistantId: [
          { required: true, message: "实验员不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getUserList();
  },
  methods: {
    /** 查询用户列表 */
    async getUserList() {
        await listUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.userList = response.rows;
          console.log(this.userList);
        }
      );
    },

    getCategoryName(categoryId) {
      switch (categoryId) {
        case 1:
          return '软件实验室';
        case 2:
          return '硬件实验室';
        case 3:
          return '网络实验室';
        default:
          return '未知类别';
      }
    },
    /** 查询实验室存储管理列表 */
    async getList() {
      this.loading = true;
      await listLaboratory(this.queryParams).then(response => {
        this.laboratoryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        labId: null,
        labNumber: null,
        labName: null,
        labCategory: null,
        equipmentCount: null,
        labAssistantId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.labId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加实验室存储管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const labId = row.labId || this.ids
      getLaboratory(labId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改实验室存储管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.labId != null) {
            updateLaboratory(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLaboratory(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const labIds = row.labId || this.ids;
      this.$modal.confirm('是否确认删除实验室存储管理编号为"' + labIds + '"的数据项？').then(function () {
        return delLaboratory(labIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('lab/laboratory/export', {
        ...this.queryParams
      }, `laboratory_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
